package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.common.utils.AssertUtils;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogDao sysLogDao;
    @Override
    public PageObject<SysLog> findPageObjects(String username, Long pageCurrent) {
        AssertUtils.isServiceValid(pageCurrent==null || pageCurrent<1,"当前页码值无效");

        int pageSize=5;
        //设置查询位置以及页面大小
        Page<SysLog> page= PageHelper.startPage(pageCurrent.intValue(),pageSize);
        //查询当前页数据
        List<SysLog>  records=sysLogDao.findPageObjects(username);
        return new PageObject<>(page.getTotal(),records,pageSize,pageCurrent);
    }

    @Override
    public int deleteObjects(Integer... ids) {

        AssertUtils.isValid(ids==null || ids.length==0, "请选择一个");
        int rows;
        try {
            rows=sysLogDao.deleteObjects(ids);
        }catch (Throwable e){
            e.printStackTrace();
            throw new ServiceException("系统正在维修");
        }

        AssertUtils.isServiceValid(rows==0, "您要删除的记录可能已经不存");

        return rows;
    }

    @Override
    public void saveObject(SysLog sysLog) {
        sysLogDao.insertObject(sysLog);
    }
}
